﻿using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.DBContext;
using PmSoft.Repositories;

namespace Modules.System.Attachment
{
    [UseDI(ServiceLifetime.Scoped)]
    public class AttachmentRepository : Repository<AttachmentInfo, DefaultDbContext>
    {
        public AttachmentRepository(DefaultDbContext dbContext, IApplicationContext applicationContext) : base(dbContext, applicationContext)
        {
        }

        /// <summary>
        /// 获取过期的临时附件
        /// </summary>
        /// <returns></returns>
        public IEnumerable<AttachmentInfo> GetAttachmentInfosByExpiredTemporary(int hours)
        {
            Sql sql = Sql.Builder;
            sql.Select("id")
                .From("sys_attachment");
            sql.Where("is_temporary=1 AND TIMESTAMPDIFF(HOUR, create_time, @0) > @1", DateTime.Now, hours);
            var ids = DbContext.FetchFirstColumn<string>(sql);

            return PopulateEntitiesByEntityIds(ids);
        }
    }
}